//
//  BirthdayDatePicker.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/1/17.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit

class BirthdayDatePicker: UITableViewCell {

    static let sharedPicker: BirthdayDatePicker = BirthdayDatePicker(frame: CGRect(x: 0, y: ScreenHeight + 10, width: ScreenWidth, height: 250))
    
    var picker: UIPickerView!
    var dateStr: String!
    var finishClosure: ((Int, Int)->Void)?
    
    var month: Int
    var day: Int
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        month = 1
        day = 1
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.color.background
        self.configSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("BirthdayDatePicker init coder")
    }
    
    private func configSubview() {
        let viewWidth = self.frame.size.width
        let viewHeight = self.frame.size.height
        picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight))
        picker.dataSource = self
        picker.delegate = self
        self.contentView.addSubview(picker)
        
        picker.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint(item: picker!, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: picker!, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: picker!, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: picker!, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
    }
    
    func configDate(_ month: Int, _ day: Int) {
        if month < 1 || day < 1 {
            self.month = 1
            self.day = 1
        } else {
            self.month = month
            self.day = day
        }
        picker.selectRow(self.month - 1, inComponent: 0, animated: false)
        picker.selectRow(self.day - 1, inComponent: 1, animated: false)
    }
    
    override var canBecomeFirstResponder: Bool {
        return true
    }
    
    override var canResignFirstResponder: Bool {
        return true
    }
    
    override func resignFirstResponder() -> Bool {
        return super.resignFirstResponder()
    }
    
}

extension BirthdayDatePicker: UIPickerViewDelegate, UIPickerViewDataSource {
    
    var months: [Int] {
        return creatInts(13)
    }
    
    var days: [Int] {
        return creatInts(31)
    }
    
    var creatInts: (Int)->[Int] {
        let closure:(Int)->[Int] = { (maxI) in
            var arr:[Int] = []
            for x in 1..<maxI {
                arr.append(x)
            }
            return arr
        }
        return closure
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return months.count
        } else if component == 1 {
            return days.count
        }
        return 0
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let num = (component==0 ? months : days)[row]

        var label:UILabel!
        if let reView = view {
            label = (reView as! UILabel)
            label.text = "\(num)"
        } else {
            label = UILabel()
            label.text = "\(num)"
            label.font = UIFont.systemFont(ofSize: 35.0)
            label.textAlignment = .center
            label.textColor = Theme.color.title
        }
        return label
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            month = months[row]
        } else if component == 1 {
            day = days[row]
        }
        if let closure = self.finishClosure {
            closure(month, day)
        }
        if self.isFirstResponder == false {
            self.becomeFirstResponder()
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 100
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    
}
